from google.appengine.api import urlfetch
import urllib
import re

login_url="http://wap.kaixin001.com/login/login.php"
house_url="/house/index.php"
garden_url="/house/garden"
ranch_url="/house/ranch"

def kxurl(suburl, userid, verify):
	url = "http://wap.kaixin001.com%s?verify=%s" % (suburl, verify)
	if userid:
		url = "%s&uid=%s" % (url, userid)
	return url

form_fields = {
  "email": "yuntao.liu@gmail.com",
  "password": "ww800224",
  "remember": 0
}

form_data = urllib.urlencode(form_fields)

result = urlfetch.fetch(url=login_url,
                        payload=form_data,
                        method=urlfetch.POST,
                        headers={'Content-Type': 'application/x-www-form-urlencoded'})

print 'Content-Type textplain'
print ''
for name in result.headers:
	print("%s = %s" % (name, result.headers[name]))

print ''
print 'login:'
print result.content

pattern=r'verify=(.*?)\"'
r = re.compile(pattern)
g = r.search(result.content).groups()
if g:
	verify = g[0]

pattern=r'uid=(.*?)\"'
r = re.compile(pattern)
g = r.search(result.content).groups()
if g:
	userid = g[0]

print "verify: %s userid: %s" % (verify, userid)

myurl = kxurl(ranch_url, userid, verify)
result = urlfetch.fetch(url=myurl, method=urlfetch.GET)

print 'ranch: '
print result.content